package com.tajiang.business.setting.activity;


import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tajiang.business.R;
import com.tajiang.business.businesscount.wallet.ConfirmPsdActivity;
import com.tajiang.business.common.activity.BaseActivity;
import com.tajiang.business.common.http.BaseResponse;
import com.tajiang.business.common.http.HttpHandler;
import com.tajiang.business.common.http.HttpResponseListener;
import com.tajiang.business.common.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 找回支付密码
 * Created by work on 2016/6/20.
 */
public class FindPsdActivity extends BaseActivity implements HttpResponseListener {

    private static final int TIME_LIMITED_SEND_CODE = 60000;  //发送校验码的时间（毫秒）
    private static final int TIME_LIMITED_PER_SECOND = 1000;  //（毫秒）


    @BindView(R.id.tv_input_phone)
    TextView tvInputPhone;
    @BindView(R.id.tv_code_verify)
    TextView tvCodeVerify;
    @BindView(R.id.tv_reset_password)
    TextView tvResetPassword;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.btn_get_code)
    Button btnGetCode;
    @BindView(R.id.rl_phone)
    RelativeLayout rlPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.rl_code)
    RelativeLayout rlCode;
    @BindView(R.id.btn_verify_code)
    Button btnVerifyCode;

    @Override
    protected void initTopBar() {
        setTitle(getResourcesString(R.string.msg_find_pay_password));
    }

    @Override
    protected void initLayout() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_password);
        ButterKnife.bind(this);
        initWidgets();
    }

    @OnClick(R.id.btn_get_code)
    public void onGetCodeClick() {
        if (check()) {
            getCode(etPhone.getText().toString());//发送取校验码
        }
    }

    @OnClick(R.id.btn_verify_code)
    public void onCheckCodeClick() {
        //TODO.......
        if (true == isCodeChecked()) {
            //此处匹配校验码
            new HttpHandler(this).verifyRegCode(etPhone.getText().toString(),
                                                etCode.getText().toString());
        } else {
            ToastUtils.showShort(getResourcesString(R.string.msg_please_input_reg_code));
        }
    }


    private boolean check() {
        if(TextUtils.isEmpty(etPhone.getText())) {
            ToastUtils.showShort(R.string.msg_input_your_phone);
            return false;
        }
        return true;
    }

    private void setWidgetsVisibility(int visibility) {
        rlCode.setVisibility(visibility);
        btnVerifyCode.setVisibility(visibility);
    }

    private void initWidgets() {
        setWidgetsVisibility(View.GONE);//隐藏校验码布局内容
        tvInputPhone.setTextColor(this.getResources().getColor(R.color.green));
    }

    private void initSendCode() {
        setWidgetsVisibility(View.VISIBLE);//显示校验码布局内容
        btnGetCode.setBackgroundColor(FindPsdActivity.this.getResources()
                .getColor(R.color.text_gray));
        tvCodeVerify.setTextColor(this.getResources().getColor(R.color.green));
    }

    private void getCode(String phone) {
        initSendCode();
        //验证码发送倒计时
        new MyCountDownTimer(TIME_LIMITED_SEND_CODE, TIME_LIMITED_PER_SECOND).start();
        //发送验证码请求
        HttpResponseListener getRegCodeListener = new HttpResponseListener() {
            @Override
            public void onSuccess(BaseResponse response) {}

            @Override
            public void onFailed(BaseResponse response) {}

            @Override
            public void onFinish() {}
        };
        new HttpHandler(getRegCodeListener).getRegCode(phone);
    }

    private boolean isCodeChecked() {
        String regCode = etCode.getText().toString().trim();
        if (regCode.equals("") || regCode.equals(null)) {
            return false;
        }
        return true;
    }

    @Override
    public void onSuccess(BaseResponse response) {
        Intent intent = new Intent();
        intent.putExtra("mode", ConfirmPsdActivity.STATE_MODE_NO_PAY_PWD);
        intent.setClass(FindPsdActivity.this, ResetPsdActivity.class);
        startActivity(intent);
        FindPsdActivity.this.finish();
    }

    @Override
    public void onFailed(BaseResponse response) {
        ToastUtils.showLong(getResourcesString(R.string.msg_error_reg_code));
    }

    @Override
    public void onFinish() {

    }

    class MyCountDownTimer extends CountDownTimer {
        /**
         * @param millisInFuture
         *      表示以毫秒为单位 倒计时的总数
         *      例如 millisInFuture=1000 表示1秒
         * @param countDownInterval
         *      表示 间隔 多少微秒 调用一次 onTick 方法
         *      例如: countDownInterval =1000 ; 表示每1000毫秒调用一次onTick()
         */
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            btnGetCode.setClickable(false);//发送验证码期间停用发送按钮
        }

        @Override
        public void onFinish() {
            btnGetCode.setText(getResourcesString(R.string.msg_send_reg_code));
            btnGetCode.setBackgroundColor(FindPsdActivity.this.getResources()
                    .getColor(R.color.green));
            btnGetCode.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            btnGetCode.setText("" + millisUntilFinished / 1000 + "s");
        }
    }

}
